package com.school.revervationSys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.revervationSys.dto.IdDto;
import com.school.revervationSys.entity.TestQuestion;
import com.school.revervationSys.service.TestQuestionService;
import com.school.revervationSys.utils.BaseResponse;
import com.school.revervationSys.utils.ErrorCode;
import com.school.revervationSys.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/testQuestion")
public class TestQuestionController {

    @Autowired
    private TestQuestionService questionService;

    @PostMapping("/addTestQuestion")
    public BaseResponse addTestQuestion(@RequestBody TestQuestion TestQuestion){

        boolean b = questionService.addTestQuestion(TestQuestion);
        if(b){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"操作失败");
    }
    @PostMapping("/updateTestQuestion")
    public BaseResponse updateTestQuestion(@RequestBody TestQuestion TestQuestion){

        boolean b = questionService.updateTestQuestion(TestQuestion);
        if(b){
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"修改失败");
    }

    @GetMapping("/delTestQuestion")
    public BaseResponse delTestQuestion(Integer testQuestionId){

        boolean b = questionService.delTestQuestion(testQuestionId);
        if(b){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"删除失败");
    }

    @GetMapping("/testQuestionInfo")
    public BaseResponse testQuestionInfo(Integer testQuestionId) {

        TestQuestion TestQuestion = questionService.testQuestionInfo(testQuestionId);

        return ResultUtils.success(TestQuestion);
    }
    @PostMapping("/batchDelTestQuestion")
    public BaseResponse batchDelTestQuestion(@RequestBody IdDto param){

        boolean b = questionService.batchDelete(param.getIds());
        if(b){
            return ResultUtils.success("批量删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_FAILED,"批量删除失败");
    }

    @GetMapping("/TestQuestionList")
    public BaseResponse TestQuestionList(@RequestParam(required = false) Integer testManageId,Integer pageNo,Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<TestQuestion> TestQuestionPage = questionService.questionList(testManageId);
        PageInfo<TestQuestion> TestQuestionPageInfo = new PageInfo<>(TestQuestionPage);
        return ResultUtils.success(TestQuestionPageInfo);
    }
    
    
}
